package com.twu.biblioteca.Command;

import com.twu.biblioteca.Bibilioteca;
import com.twu.biblioteca.BibiliotecaModel;

import java.io.PrintWriter;

public class ReserveBookCommand implements IBibiliotecaCommand {
    private Bibilioteca bibilioteca;

    public ReserveBookCommand(Bibilioteca bibilioteca) {
        this.bibilioteca = bibilioteca;
    }

    public void ICommand(PrintWriter outputBuffer) {
        reserveBook();
    }

    private void reserveBook() {
        if (!bibilioteca.isLogged()) {
            bibilioteca.getOutputBuffter().println("Please login first");
            return;
        }
        int booknum = bibilioteca.getScanner().nextInt();
        if (invalidBookSelection(booknum)) {
            bibilioteca.getOutputBuffter().println("Sorry the book you choose does not exist");
            return;
        }
        if (BibiliotecaModel.getBooks().get(booknum - 1).reserve())
            bibilioteca.getOutputBuffter().println("Thank You! Enjoy the book");
        else
            bibilioteca.getOutputBuffter().println("Sorry we don't have that book yet");
    }

    private boolean invalidBookSelection(int booknum) {
        if (booknum <= 0 || booknum > BibiliotecaModel.getBooks().size())
            return true;
        return false;
    }

}
